local yonghui = fk.CreateSkill{
  name = "rfenghou_3d18j__yonghui",
}

Fk:addTargetTip{
  name = yonghui.name,
  target_tip = function (self, player, to_select, selected, _, _, selectable, extra_data)
    if not selectable then return end
    if selected[1] == to_select then
      return "draw1"
    elseif selected[2] == to_select then
      return "discard1"
    end
  end
}
yonghui:addEffect(fk.EventPhaseStart, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(yonghui.name) and player.phase == Player.Play
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = room:askToChoosePlayers(player, {
      min_num = 2,
      max_num = 2,
      targets = room:getAlivePlayers(false),
      cancelable = true,
      skill_name = yonghui.name,
      prompt = "#rfenghou_3d18j__yonghui-invoke",
      target_tip_name = yonghui.name,
    })
    if #targets < 2 then return end
    event:setCostData(self, { tos = targets })
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local tos = event:getCostData(self).tos
    if not tos or #tos < 2 then return end
    room:addPlayerMark(tos[1], "@rfenghou_3d18j__yonghui_draw-phase")
    room:addPlayerMark(tos[2], "@rfenghou_3d18j__yonghui_discard-phase")
    data.extra_data = data.extra_data or {}
    data.extra_data.rfenghou_3d18j__yonghui_data = data.extra_data.rfenghou_3d18j__yonghui_data or {}
    data.extra_data.rfenghou_3d18j__yonghui_data[player] = {
      {tos[1], 1},
      {tos[2], 1}
    }
  end
})
yonghui:addEffect(fk.AfterCardsMove, {
  is_delay_effect = true,
  mute = true,
  can_refresh = function(self, event, target, player, data)
    local room = player.room
    local phase_event = room.logic:getMostRecentEvent(GameEvent.Phase)
    if not (phase_event and phase_event.data.extra_data and
    phase_event.data.extra_data.rfenghou_3d18j__yonghui_data and
    phase_event.data.extra_data.rfenghou_3d18j__yonghui_data[player]) then
      return false
    end
    for _, move in ipairs(data) do
      if move.toArea == Card.DiscardPile then
        for _, info in ipairs(move.moveInfo) do
          if Fk:getCardById(info.cardId).suit == Card.Club then
            return true
          end
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local phase_event = room.logic:getMostRecentEvent(GameEvent.Phase)
    local yonghui_data = phase_event.data.extra_data.rfenghou_3d18j__yonghui_data[player]
    local yonghui_targets = table.map(yonghui_data, function(v) return v[1] end)
    local to = room:askToChoosePlayers(player, {
      targets = yonghui_targets,
      min_num = 1,
      max_num = 1,
      skill_name = yonghui.name,
      prompt = "#rfenghou_3d18j__yonghui-add",
    })[1]
    if not to then return end
    local yonghui_index = table.indexOf(yonghui_targets, to)
    if not yonghui_index then return end
    if yonghui_index == 2 then
      room:addPlayerMark(to, "@rfenghou_3d18j__yonghui_discard-phase")
    elseif yonghui_index == 1 then
      room:addPlayerMark(to, "@rfenghou_3d18j__yonghui_draw-phase")
    end
    yonghui_data[yonghui_index][2] = yonghui_data[yonghui_index][2] + 1
  end,
})
yonghui:addEffect(fk.EventPhaseEnd, {
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return data.extra_data and
    data.extra_data.rfenghou_3d18j__yonghui_data and
    data.extra_data.rfenghou_3d18j__yonghui_data[player]
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local yonghui_data = data.extra_data.rfenghou_3d18j__yonghui_data[player]
    local lose_hp_count = 0
    for _, info in ipairs(yonghui_data) do
      local to = info[1]
      local count = info[2]
      if to:getMark("@rfenghou_3d18j__yonghui_draw-phase") > 0 then
        room:removePlayerMark(to, "@rfenghou_3d18j__yonghui_draw-phase", count)
        room:drawCards(to, count, self.name)
      end
      if to:getMark("@rfenghou_3d18j__yonghui_discard-phase") > 0 then
        room:removePlayerMark(to, "@rfenghou_3d18j__yonghui_discard-phase", count)
        room:askToDiscard(to, {
          min_num = count,
          max_num = count,
          skill_name = yonghui.name,
        })
      end
    end
    if player:isAlive() then
      for _, info in ipairs(yonghui_data) do
        local to = info[1]
        if to.hp ~= to:getHandcardNum() then
          lose_hp_count = lose_hp_count + 1
        end
      end
      if lose_hp_count > 0 then
        room:loseHp(player, lose_hp_count, yonghui.name)
      end
    end
  end
})

Fk:loadTranslationTable{
  ["rfenghou_3d18j__yonghui"] = "用晦",
  [":rfenghou_3d18j__yonghui"] = "出牌阶段开始时，你可以指定两名角色，令其于此阶段结束时分别摸或弃置一张牌；此阶段内梅花牌进入弃牌堆时，你可以令其中一项数值加一。结算后其中每有一名角色手牌数与体力值不相等，你失去1点体力。",

  ["#rfenghou_3d18j__yonghui-invoke"] = "用晦：你可以指定两名角色，令其于此阶段结束时分别摸或弃置一张牌",
  ["#rfenghou_3d18j__yonghui-add"] = "用晦：你可以令一名角色多摸或多弃一张牌",

  ["@rfenghou_3d18j__yonghui_draw-phase"] = "用晦 摸",
  ["@rfenghou_3d18j__yonghui_discard-phase"] = "用晦 弃",
}

return yonghui
